home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / jpeglibrary / examples / jpeg.library_load / load_file.c next >
Encoding:
C/C++ Source or Header  |  1998-05-26  |  4.4 KB  |  163 lines

  1. /* This example use of jpeg.library loads the jpeg file specified
  2.         on the command line and views it halved in size on a cybergraphics
  3.         screen. It uses an RGB triplet buffer tag to store the image data
  4.         from jpeg.library. This example uses an AmigaDOS file pointer for the
  5.         source stream.
  6.  
  7.         Supports RGB and Grayscale source jpeg images.
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #include <dos/dos.h>
  13. #include <exec/types.h>
  14.  
  15. #include <clib/dos_protos.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/cybergraphics_protos.h>
  18. #include <clib/intuition_protos.h>
  19.  
  20. #include <pragmas/dos_pragmas.h>
  21. #include <pragmas/exec_pragmas.h>
  22. #include <pragmas/intuition_pragmas.h>
  23. #include <pragmas/cybergraphics_pragmas.h>
  24.  
  25. #include <cybergraphics/cybergraphics.h>
  26.  
  27. #include <jpeg/jpeg.h>
  28. #include <jpeg/jpeg_protos.h>
  29. #include <jpeg/jpeg_pragmas.h>
  30.  
  31. extern struct Library *DOSBase;
  32. struct Library *JpegBase, *IntuitionBase;
  33. struct CyberGfxBase *CyberGfxBase = NULL;
  34.  
  35. void main( int argc, char **argv )
  36. {
  37.     JpegBase = OpenLibrary( "jpeg.library", NULL );
  38.     IntuitionBase = OpenLibrary( "intuition.library", NULL );
  39.     CyberGfxBase = (struct CyberGfxBase *)OpenLibrary( "cybergraphics.library", 0L );
  40.  
  41.     if ( IntuitionBase != NULL && CyberGfxBase != NULL && JpegBase != NULL )
  42.     {
  43.         ULONG err;
  44.         ULONG DisplayID;
  45.         struct Screen *scr;
  46.         struct Window *win;
  47.         struct Message *msg;
  48.         struct JPEGDecHandle *jph;
  49.         UBYTE *buffer;
  50.         ULONG x, y;
  51.         BPTR fp;
  52.         UBYTE colourspace;
  53.         ULONG bpp;
  54.  
  55.         fp = Open( argv[1], MODE_OLDFILE );
  56.         if ( fp != NULL )
  57.         {
  58.             err = AllocJPEGDecompress( &jph, JPG_SrcFile, fp, TAG_DONE );
  59.             if ( !err )
  60.             {
  61.                 err = GetJPEGInfo( jph, JPG_Width, &x, JPG_Height, &y,
  62.                     JPG_ColourSpace, &colourspace,
  63.                     JPG_BytesPerPixel, &bpp,
  64.                     JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  65.                     TAG_DONE );
  66.                 if ( !err )
  67.                 {
  68.                     printf( "colourspace=%d\n", colourspace );
  69.                     printf( "bytes per pixel=%d\n", bpp );
  70.                     printf( "width=%d\n", x );
  71.                     printf( "height=%d\n", y );
  72.  
  73.                     buffer = AllocRGBFromJPEG( jph,
  74.                         JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  75.                         TAG_DONE );
  76.                     if ( buffer != NULL )
  77.                     {
  78.                         err = DecompressJPEG( jph,
  79.                             JPG_DestRGBBuffer, buffer,
  80.                             JPG_ScaleNum, 1, JPG_ScaleDenom, 2,
  81.                             TAG_DONE );
  82.                         if ( !err )
  83.                         {
  84.                             DisplayID = BestCModeIDTags( CYBRBIDTG_NominalWidth, 640,
  85.                                 CYBRBIDTG_NominalHeight, 480,
  86.                                 CYBRBIDTG_Depth, 24,
  87.                                 TAG_DONE );
  88.  
  89.                             if ( DisplayID != INVALID_ID )
  90.                             {
  91.                                 scr = OpenScreenTags( NULL,
  92.                                     SA_Title, "Proof",
  93.                                     SA_DisplayID, DisplayID,
  94.                                     SA_Depth, GetCyberIDAttr( CYBRIDATTR_DEPTH, DisplayID ),
  95.                                     TAG_DONE );
  96.  
  97.                                 if ( scr != NULL )
  98.                                 {
  99.                                     win = OpenWindowTags( NULL,
  100.                                         WA_Title, "Proof",
  101.                                         WA_Flags, WFLG_ACTIVATE | WFLG_SIMPLE_REFRESH |
  102.                                             WFLG_SIZEGADGET | WFLG_RMBTRAP | WFLG_DRAGBAR |
  103.                                             WFLG_DEPTHGADGET | WFLG_CLOSEGADGET,
  104.                                         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  105.                                             IDCMP_SIZEVERIFY | IDCMP_NEWSIZE | IDCMP_RAWKEY,
  106.                                         WA_Left, 16,
  107.                                         WA_Top, scr->BarHeight+16,
  108.                                         WA_Width, x,
  109.                                         WA_Height, y,
  110.                                         WA_CustomScreen, scr,
  111.                                         TAG_DONE );
  112.  
  113.                                     if ( win != NULL )
  114.                                     {
  115.                                         UBYTE format = RECTFMT_RGB;
  116.                                         UWORD rowwidth = x * 3;
  117.  
  118.                                         switch ( colourspace )
  119.                                         {
  120.                                             case JPCS_UNKNOWN:
  121.                                             case JPCS_GRAYSCALE:
  122.                                                 format = RECTFMT_GREY8;
  123.                                                 rowwidth = x * bpp;
  124.                                             break;
  125.                                         }
  126.  
  127.                                         WritePixelArray( buffer, 0, 0, rowwidth, win->RPort, 0, 0, x, y, format );
  128.  
  129.                                         Wait( 1L << win->UserPort->mp_SigBit );
  130.                                         while ( ( msg = GetMsg( win->UserPort ) ) != NULL ) ReplyMsg( msg );
  131.  
  132.                                         CloseWindow( win );
  133.                                     }
  134.                                     else printf( "failed to open window\n" );
  135.  
  136.                                     CloseScreen( scr );
  137.                                 }
  138.                                 else printf( "failed to open screen\n" );
  139.                             }
  140.                             else printf( "failed to get display id\n" );
  141.                         }
  142.                         else printf( "decompress jpeg error:%d\n", err );
  143.  
  144.                         FreeJPEGRGBBuffer( buffer );
  145.                     }
  146.                     else printf( "cant allocate rgb buffer\n" );
  147.                 }
  148.                 else printf( "get jpeg info error:%d\n", err );
  149.  
  150.                 FreeJPEGDecompress( jph );
  151.             }
  152.             else printf( "alloc jpeg error:%d\n", err );
  153.  
  154.             Close( fp );
  155.         }
  156.         else printf( "cant open file\n" );
  157.     }
  158.  
  159.     if ( JpegBase != NULL ) CloseLibrary( JpegBase );
  160.     if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  161.     if ( CyberGfxBase ) CloseLibrary ( (struct Library *)CyberGfxBase );
  162. }
  163.